package com.cz.modules.jxc.service.impl;

import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cz.common.utils.PageUtils;
import com.cz.common.utils.Query;

import com.cz.modules.jxc.dao.JxcSellDetailsDao;
import com.cz.modules.jxc.entity.JxcSellDetailsEntity;
import com.cz.modules.jxc.service.JxcSellDetailsService;


@Service("jxcSellDetailsService")
public class JxcSellDetailsServiceImpl extends ServiceImpl<JxcSellDetailsDao, JxcSellDetailsEntity> implements JxcSellDetailsService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<JxcSellDetailsEntity> page = this.page(
                new Query<JxcSellDetailsEntity>().getPage(params),
                new QueryWrapper<JxcSellDetailsEntity>()
        );
        return new PageUtils(page);
    }
    @Override
    public List<JxcSellDetailsEntity> detailsbytime(String startTime, String endTime) {
        List<JxcSellDetailsEntity> list = new ArrayList<>();
        //查询该时间段的所有销售商品
        List<JxcSellDetailsEntity> detaillist = this.list(
                new QueryWrapper<JxcSellDetailsEntity>()
                        .ge("sell_master_date", startTime)
                        .le("sell_master_date", endTime)
        );
        List<JxcSellDetailsEntity> detailgrouplist = this.list(
                new QueryWrapper<JxcSellDetailsEntity>()
                        .ge("sell_master_date", startTime)
                        .le("sell_master_date", endTime)
                        .groupBy("commodity_barcode")
        );
        for (JxcSellDetailsEntity gou : detailgrouplist) {
            int count = 0;
            for (JxcSellDetailsEntity detail : detaillist) {
                if (detail.getCommodityBarcode().equals(gou.getCommodityBarcode())) {
                    count += detail.getSellCount();
                }
            }
            JxcSellDetailsEntity en = new JxcSellDetailsEntity();
            en.setYValue(gou.getCommodityName());
            en.setXValue(count);
            list.add(en);
        }
        //通过销量 排序
        Collections.sort(list, new Comparator<JxcSellDetailsEntity>() {
            @Override
            public int compare(JxcSellDetailsEntity o1, JxcSellDetailsEntity o2) {
                int count1= o1.getXValue();
                int count2 = o2.getXValue();
                if(count1<count2){
                    return 1;
                }else if(count1>count2){
                    return -1;
                }else{
                    return 0;
                }
            }
        });
        if(list.size()>10){
            List<JxcSellDetailsEntity> list1=new ArrayList<>();
            for (int i = 0; i <10 ; i++) {
                list1.add(list.get(i));
            }
            return list1;
        }else{
            return list;
        }
    }
    @Override
    public List<JxcSellDetailsEntity> detailsRanking() {
        List<JxcSellDetailsEntity> list = new ArrayList<>();
        String today =new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        today +=" 00:00:00";
        //查询当天的所有销售商品
        List<JxcSellDetailsEntity> detaillist = this.list(
                new QueryWrapper<JxcSellDetailsEntity>()
                        .ge("sell_master_date", today)
        );
        //查询当天的所有销售商品种类 通过条形码 分组
        List<JxcSellDetailsEntity> detailgrouplist = this.list(
                new QueryWrapper<JxcSellDetailsEntity>()
                        .ge("sell_master_date", today)
                        .groupBy("commodity_barcode")
        );
        for (JxcSellDetailsEntity gou : detailgrouplist) {
            int count = 0;
            for (JxcSellDetailsEntity detail : detaillist) {
                if (detail.getCommodityBarcode().equals(gou.getCommodityBarcode())) {
                    count += detail.getSellCount();
                }
            }
            JxcSellDetailsEntity en = new JxcSellDetailsEntity();
            en.setYValue(gou.getCommodityName());
            en.setXValue(count);
            list.add(en);
        }
        //通过销量 排序
        Collections.sort(list, new Comparator<JxcSellDetailsEntity>() {
            @Override
            public int compare(JxcSellDetailsEntity o1, JxcSellDetailsEntity o2) {
                int count1= o1.getXValue();
                int count2 = o2.getXValue();
                if(count1<count2){
                    return 1;
                }else if(count1>count2){
                    return -1;
                }else{
                    return 0;
                }
            }
        });
        if(list.size()>10){
            List<JxcSellDetailsEntity> list1=new ArrayList<>();
            for (int i = 0; i <10 ; i++) {
                list1.add(list.get(i));
            }
            return list1;
        }else{
            return list;
        }

    }

    @Override
    public List<JxcSellDetailsEntity> querydetail(Map<String, Object> params) {
        String ordercode= (String) params.get("id");
        List<JxcSellDetailsEntity> list=this.list(
                new QueryWrapper<JxcSellDetailsEntity>()
                .eq("sell_master_code",ordercode)
        );
        return list;
    }

    @Override
    public List xslTop10(String startTime, String endTime) {

        List<JxcSellDetailsEntity> sellList=this.baseMapper.xslTop10(startTime, endTime);
        List<String> nameList=new ArrayList();
        List<String> valueList=new ArrayList();
        for (JxcSellDetailsEntity sell:sellList) {
            nameList.add(sell.getCommodityName());
            valueList.add(sell.getCountValue());
        }
        Collections.reverse(nameList);
        Collections.reverse(valueList);
        List<List<String>> list=new ArrayList<>();
        list.add(nameList);
        list.add(valueList);
        return list;
    }

    @Override
    public List xseTop10(String startTime, String endTime) {

        List<JxcSellDetailsEntity> sellList=this.baseMapper.xseTop10(startTime, endTime);
        List<String> nameList=new ArrayList();
        List<String> valueList=new ArrayList();
        for (JxcSellDetailsEntity sell:sellList) {
            nameList.add(sell.getCommodityName());
            valueList.add(sell.getCountValue());
        }
        Collections.reverse(nameList);
        Collections.reverse(valueList);
        List<List<String>> list=new ArrayList<>();
        list.add(nameList);
        list.add(valueList);
        return list;
    }


}
